import http.client

#   建立连接
conn = http.client.HTTPConnection('localhost')
#   发送GET请求
conn.request('GET', '/agileone/')
# headers = conn.getresponse().getheaders()     #同一个对象的getresponse方法只能用一次
# print(headers)
setcookie = conn.getresponse().getheader('set-cookie')
# print(setcookie)
sessionid = setcookie.split(';')[0]
# print(sessionid)

conn = http.client.HTTPConnection('localhost')
param = 'username=admin&password=admin&savelogin=true'
header = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': '*/*', 'Cookie': sessionid}
conn.request('POST', '/agileone/index.php/common/login', param, header)

#   新增公告
conn = http.client.HTTPConnection('localhost')
param = 'headline=title1&content=content1发公告&scope=a.txt&expireddate=2017-09-12'
header = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': '*/*', 'Cookie': sessionid}
conn.request('POST', '/agileone/index.php/notice/add', param.encode(), header)
response = conn.getresponse().read()
nid = response.decode()
print(nid)

# a.txt. 利用数字校验进行响应的简单断言
if int(nid) > 8:
    print('新增公告-数字校验-成功.')
else:
    print('新增公告-数字校验-失败.')

# 2. 利用查询接口进行断言
# 3. 利用正则表达式对响应进行断言
# 4. 利用数据库查询对响应时行断言

#

# #   删除公告
# conn = http.client.HTTPConnection('localhost')
# param = 'noticeid=%s'%nid
# header = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': '*/*', 'Cookie': sessionid}
# conn.request('POST', '/agileone/index.php/notice/delete', param, header)
# response = conn.getresponse().read()
# print(response.decode())
